package com.liesheng.haylou.ui.Login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.google.gson.Gson;
import com.liesheng.haylou.R;
import com.liesheng.haylou.app.HyApplication;
import com.liesheng.haylou.base.BaseActivity;
import com.liesheng.haylou.base.BaseFunActivity;
import com.liesheng.haylou.base.BaseResult;
import com.liesheng.haylou.base.BaseVm;
import com.liesheng.haylou.bean.LoginBean;
import com.liesheng.haylou.bean.UserUpdateBean;
import com.liesheng.haylou.databinding.ActivityLoginBinding;
import com.liesheng.haylou.db.entity.BoundedDevice;
import com.liesheng.haylou.event.DelDeviceEvent;
import com.liesheng.haylou.event.NickNameEvent;
import com.liesheng.haylou.net.HttpCallback;
import com.liesheng.haylou.net.HttpRequest;
import com.liesheng.haylou.ui.Register.RegisterActivity;
import com.liesheng.haylou.ui.main.MainActivity;
import com.liesheng.haylou.ui.reset.ResetPswActivity;
import com.liesheng.haylou.utils.FireGsonUtil;
import com.liesheng.haylou.utils.ToastUtil;
import com.liesheng.haylou.utils.sp.SpUtil;
import com.sahooz.library.Country;
import com.sahooz.library.PickActivity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;


/**
 * Date:${DATA}
 * Author ${ZhangZhiQiang}
 * Description
 */
public class LoginActivity extends BaseActivity<ActivityLoginBinding, BaseVm> {
    List<NickNameEvent> nickNameInfo;
    private boolean iscleartext = false;
    private InputMethodManager imm;
    String loginname;
    String password;
    String countryCode;
    Intent intent;
    Country country;

    @Override
    protected ActivityLoginBinding getmBinding() {
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_login, null, false);
        return mBinding;
    }

    @Override
    protected BaseVm getViewModel() {
        return null;
    }

    @Override
    protected void initData() {
        showHeadLayout(false);
        mBinding.tvCountry.setVisibility(View.VISIBLE);

    }

    @Override
    public void onClickView(View view) {
        switch (view.getId()) {
            case R.id.ivSpiner:
                intent = new Intent(this, PickActivity.class);
                startActivityForResult(intent, 111);
                break;
            case R.id.btPsd:
                if (TextUtils.isEmpty(password)) {
                    ToastUtil.showToast("请先输入密码!");
                    return;
                }
                if (!iscleartext) {
                    mBinding.btPsd.setImageResource(R.drawable.login_ico_pwd_dide);
                    mBinding.etPsd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    mBinding.btPsd.setImageResource(R.drawable.login_ico_pwd_show);
                    mBinding.etPsd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                break;
            case R.id.tvForgetPs:
                ResetPswActivity.startBy(LoginActivity.this);
                break;
            case R.id.tvNorReg:
                RegisterActivity.startBy(LoginActivity.this);
                break;
            case R.id.btLogin:
                hideSoftKeyBoard();
                login();//登录
            default:
                break;
        }
    }

    private void login() {
        loginname = mBinding.etPhone.getText().toString();
        password = mBinding.etPsd.getText().toString();
        if (TextUtils.isEmpty(countryCode) && country != null) {
            countryCode = "" + country.code;
        }
        if (TextUtils.isEmpty(countryCode)) {
            ToastUtil.showToast("请选择区号！");
            return;
        }
        if (TextUtils.isEmpty(loginname)) {
            ToastUtil.showToast("请输入手机号码！");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtil.showToast("请先输入密码!");
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userPhone", loginname);
        map.put("password", password);
        map.put("countryCode", countryCode);
        requestHttp(buildHttpService().login(HttpRequest.getBody(map)), new HttpCallback<LoginBean>() {
            @Override
            public void onNext(LoginBean loginBean) {
                if (null != loginBean && loginBean.getCode() == 200) {
                    SpUtil.saveToken(loginBean.getData().getToken());//保存token
                    MainActivity.startBy(LoginActivity.this);
                    ToastUtil.showToast("恭喜你，登录成功");
                    finish();

                }
            }
        });
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventPost(NickNameEvent event) {
        if (nickNameInfo  !=null){
        }
    }
    @Override
    protected void onActivityResult(int requestcode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestcode, resultCode, data);
        if (requestcode == 111 && resultCode == Activity.RESULT_OK) {
            country = Country.fromJson(data.getStringExtra("country"));
            if (country.flag != 0)
                mBinding.tvCountry.setText("+" + country.code);
        }
    }


    public static void startBy(BaseFunActivity activity) {
        Intent intent = new Intent(activity, LoginActivity.class);
        activity.startActivity(intent);
    }

    public void hideSoftKeyBoard() {
        View localView = getCurrentFocus();
        if (this.imm == null) {
            this.imm = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
        }
        if ((localView != null) && (this.imm != null)) {
            this.imm.hideSoftInputFromWindow(localView.getWindowToken(), 2);
        }
    }


}
